package Simulator;

import java.util.Iterator;
import java.util.List;

public class Hunter extends AbstractHuman implements ActorInterface {

	public void act(Field currentField, Field updatedField,
			List<ActorInterface> newActors) {
		// Move towards the source of animals if found.
		Location newLocation = huntAnimal(currentField, getLocation());
		if (newLocation == null) { // no animal found - move randomly
			newLocation = updatedField.freeAdjacentLocation(getLocation());
		}
		if (newLocation != null) {
			setLocation(newLocation);
			updatedField.place(this); // sets location
		}
	}

	private Location huntAnimal(Field field, Location location) {
		Iterator<Location> adjacentLocations = field
				.adjacentLocations(location);
		Location where = null;
		while (adjacentLocations.hasNext()) {
			where = adjacentLocations.next();
			ActorInterface actor = field.getActorAt(where);
			if (actor instanceof AbstactAnimal) {
				AbstactAnimal animal = (AbstactAnimal) actor;
				if (animal.isAlive()) {
					animal.setDead();
				}
			}
		}
		return where;
	}
}
